<?php

namespace app\listener\notify;


use app\event\SupplierNotifyFail;
use app\model\CouponOrder;


/**
 * 供应商退款 统一在这处理  ps:其他的业务逻辑不要在这里写，可单独起个事件监听
 * 退款状态
 */
class CouponOrderUnionRefund
{

    /**
     * 事件监听处理
     * @param SupplierNotifyFail $event
     * @return mixed
     */
    public function handle($event)
    {
        $couponOrder = $event->couponOrder;

        if ($couponOrder->serv_type == PRODUCT_COUPON_SERV) {
            return true;
        }

        // 小类型配置
        if ($couponOrder->type != CouponOrder::TYPE_COUPON_SELF_EMPLOYED) {
            return true;
        }

        if ($couponOrder->refund_status != CouponOrder::C_REFUND_STATUS_NO) {
            return true;
        }

        $this->refundStatus($couponOrder);
    }


    /**
     * 更新退款状态
     * @param CouponOrder $couponOrder
     * @return bool
     */
    public function refundStatus(CouponOrder $couponOrder)
    {
        // 退款
        $couponOrder->refund_status = CouponOrder::C_REFUND_STATUS_ING;

        if (empty($couponOrder->refund_amount)) {
            // 默认取支付金额
            $couponOrder->refund_amount = $couponOrder->pay_amount;
        }

        $couponOrder->save();

    }

}